package org.gosdt.ew.model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import org.gosdt.ew.db.EWDataBase;

/**
 * Class describe Profile entity allow db access
 * 
 * @author anton
 * @version 0.01
 */
public class Profiles extends DataBaseAccess {

	private String name;
	private int askCount = 0;
	private int restTime = 0;
	private String nativeLanguage = "";
	private String studyLanguage = "";
	private boolean loadByDefault = false;

	public Profiles(String profileName) {
		super("Profiles");
		name = profileName;
	}

	public Profiles getProfileByName(String name) {
		HashMap<String, String> query = new HashMap<String, String>();
		
		query.put("name", name);
		ArrayList<Profiles> list = getList(query);
		if (list.size() > 0)
			return list.get(0);
		else
			return null;
	}

	public int getAskCount() {
		return askCount;
	}

	public int getRestTime() {
		return restTime;
	}

	public String getName() {
		return name;
	}

	public String getNativeLanguage() {
		return nativeLanguage;
	}

	public String getStudyLanguage() {
		return studyLanguage;
	}

	public void setAskCount(int count) {
		askCount = count;
	}

	public void setRestTime(int seconds) {
		restTime = seconds;
	}

	public void setNativeLanguage(Languages lang) {
		nativeLanguage = lang.getName();
	}

	public void setStudyLanguage(Languages lang) {
		studyLanguage = lang.getName();
	}

	public void setLoadByDefault(boolean isDefault) {
		loadByDefault = isDefault;
	}

	@Override
	protected final ResultSet getListByDetail(HashMap<?, ?> keys) {
		String selectSQL = "SELECT * From Profiles where";
		int keysCount = keys.size();
		if (keysCount == 0) {
			selectSQL += " 1 = 1";
		} else {
			if (keys.get("name") != null) {
				selectSQL += " name = '" + keys.get("name") + "'";
				if (keysCount > 1) {
					selectSQL += " and ";
					keysCount--;
				}
			}
			;
			if (keys.get("nativeLanguage") != null) {
				selectSQL += " nativeLanguage = '" + keys.get("nativeLanguage")
						+ "'";
				if (keysCount > 1) {
					selectSQL += " and ";
					keysCount--;
				}
			}
			;
			if (keys.get("stadyLanguage") != null) {
				selectSQL += " stadyLanguage = '" + keys.get("stadyLanguage")
						+ "'";
				if (keysCount > 1) {
					selectSQL += " and ";
					keysCount--;
				}
			}
			;
			if (keys.get("loadByDefoult") != null) {
				selectSQL += " loadByDefoult = " + keys.get("loadByDefoult");
				if (keysCount > 1) {
					selectSQL += " and ";
					keysCount--;
				}
			}
		}
		;

		try {
			EWDataBase db = EWDataBase.getInstance();
			return db.getConn().prepareStatement(selectSQL).executeQuery();
		} catch (SQLException ex) {
			throw new RuntimeException(ex);
		}
	}

	public Profiles getProfileLoadedByDefault() {
		HashMap<String, String> def = new HashMap<String, String>();
		def.put("loadByDefoult", "true");
		ArrayList<Profiles> defList = getList(def);
		if (defList.size() > 0) {
			return defList.get(0);
		} else {
			return null;
		}
	}

	public ArrayList<Profiles> getAllProfilesInDB() {
		return getList(new HashMap<String, String>());
	}

	private ArrayList<Profiles> getList(HashMap<String, String> keys) {
		ArrayList<Profiles> list = new ArrayList<Profiles>();
		try {
			ResultSet rs = getListByDetail(keys);
			while (rs.next()) {
				Profiles item = new Profiles(rs.getString("name"));
				item.askCount = rs.getInt("askCount");
				item.restTime = rs.getInt("restTime");
				item.nativeLanguage = rs.getString("nativeLanguage");
				item.studyLanguage = rs.getString("stadyLanguage");
				item.loadByDefault = rs.getBoolean("loadByDefoult");
				list.add(item);
			}
			rs.close();
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	public int insertCurRecord() {
		if (getProfileByName(name) != null)
			return updateCurrentRecord();
		HashMap<String, String> insertValues = new HashMap<String, String>();
		insertValues.put("name".toUpperCase(), name);
		insertValues.put("restTime".toUpperCase(),
				((Integer) restTime).toString());
		insertValues.put("askCount".toUpperCase(),
				((Integer) askCount).toString());
		insertValues.put("nativeLanguage".toUpperCase(), nativeLanguage);
		insertValues.put("stadyLanguage".toUpperCase(), studyLanguage);
		insertValues.put("loadByDefoult".toUpperCase(),
				((Boolean) loadByDefault).toString());
		return super.insertOneRecord(insertValues);
	}

	public int updateCurrentRecord() {
		ArrayList<String> where = new ArrayList<String>();
		where.add("name = '" + name + "'");

		ArrayList<String> sets = new ArrayList<String>();
		sets.add("restTime = " + ((Integer) restTime).toString());
		sets.add("askCount = " + ((Integer) askCount).toString());
		sets.add("nativeLanguage = '" + nativeLanguage + "'");
		sets.add("stadyLanguage = '" + studyLanguage + "'");
		sets.add("loadByDefoult = " + loadByDefault);

		return super.updateOneRecord(where, sets);
	}

	public int deleteProfile() {
		ArrayList<String> keys = new ArrayList<String>();
		keys.add("name = '" + name + "'");

		return super.deleteOneRecord(keys);
	}

	public void setAsDefaultProfile() {
		String dropDefaultState = "UPDATE Profiles SET loadByDefoult = false WHERE loadByDefoult = true";
		String setDefaultState = "UPDATE Profiles SET loadByDefoult = true WHERE name = '"
				+ name + "'";
		try {
			db.getConn().prepareStatement(dropDefaultState).execute();
			db.getConn().prepareStatement(setDefaultState).execute();
		} catch (SQLException except) {
			except.printStackTrace();
		}
	}

}
